#ifndef FFB_PROTOCOL_H
#define FFB_PROTOCOL_H

#include <stdint.h>
#include <stdbool.h>

// 命令字定义
#define CMD_RESET_DEVICE         0x01
#define CMD_GET_DEVICE_INFO      0x02
#define CMD_DEVICE_INFO_RESP     0x03
#define CMD_SAVE_PARAMS          0x04
#define CMD_OPERATION_RESULT     0x05
#define CMD_GET_ALL_PARAMS       0x10
#define CMD_PARAMS_RESP          0x11
#define CMD_SET_SPRING_PARAMS    0x12
#define CMD_SET_DAMPER_PARAMS    0x13
#define CMD_SET_WHEEL_CENTER     0x16
#define CMD_PLAY_TEST_EFFECT     0x30
#define CMD_STOP_ALL_EFFECTS     0x31
#define CMD_REALTIME_FORCE       0x32
#define CMD_EFFECT_STATUS        0x33
#define CMD_ENABLE_DATA_STREAM   0x50
#define CMD_DISABLE_DATA_STREAM  0x51
#define CMD_POSITION_DATA        0x52
#define CMD_FORCE_OUTPUT_DATA    0x53
#define CMD_DEVICE_STATUS        0x54

// 帧头定义
#define PROTOCOL_HEADER          0xAA55

// 错误代码
#define ERR_SUCCESS              0x00
#define ERR_INVALID_CMD          0x01
#define ERR_INVALID_DATA         0x02
#define ERR_DEVICE_BUSY          0x03
#define ERR_FLASH_WRITE          0x04

typedef enum {
    DATA_STREAM_NONE = 0,
    DATA_STREAM_POSITION = 1,
    DATA_STREAM_FORCE = 2,
    DATA_STREAM_STATUS = 4,
    DATA_STREAM_ALL = 7
} DataStreamType_t;

#pragma pack(push, 1)

// 协议帧结构
#pragma pack(push, 1)
typedef struct {
    uint16_t header;
    uint8_t command;
    uint8_t data_length;
    uint8_t data[];  // 柔性数组
} ProtocolFrame_t;
#pragma pack(pop)

#pragma pack(pop)


typedef struct {
    uint8_t  fw_version[4];    // 固件版本号
    uint16_t device_id;        // 设备ID
    uint8_t  hardware_rev;     // 硬件版本
    uint32_t serial_number;    // 序列号
    uint16_t max_force;        // 最大输出力(mN)
    uint8_t  axis_count;       // 轴数
} __attribute__((packed)) DeviceInfo_t;

typedef struct {
    int8_t Axis1Dir; //轴1的方向
    // int8_t Axis2Dir; //轴2的方向
    float FFB_Gain;//设备力反馈增益
    uint32_t XAngleLimit;
    uint32_t YAngleLimit; //最大角度
    uint8_t damp; //阻尼
    uint8_t friction;//摩擦
    uint8_t spring; //回中
    uint8_t inertia; //惯性
    
} __attribute__((packed)) ConfigList;
// 函数声明
void protocol_init(void); //协议解析器初始化函数
bool protocol_process_rx_data(uint8_t* data, uint32_t length);
void protocol_send_response(uint8_t command, uint8_t* data, uint8_t length);
void protocol_send_data_stream(void);
#endif